//
//  ScenarioAnimatedRefreshAPNGConfiguration.m
//  TravelGuideMdd
//
//  Created by HanFeng on 5/12/14.
//  Copyright (c) 2014 mafengwo.com. All rights reserved.
//

#import "ScenarioAnimatedRefreshAPNGConfiguration.h"

@implementation ScenarioAnimatedRefreshAPNGConfiguration

- (CGFloat)scenario_frameInterval
{
    if (self.scenario_fps == 0) {
        return 1.f/30.f;
    }
    return 1.f/self.scenario_fps;
}

- (NSTimeInterval)scenario_2_release_min_duration
{
    return (self.scenario_2_release_end_frame - self.scenario_2_release_start_frame) * self.scenario_frameInterval;
}

- (BOOL)isOutOfDate
{
    NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
    if ((self.startTime == 0 || self.startTime < now)
        && (self.endTime == 0 || now < self.endTime)) {
        return YES;
    }
    return NO;
}

- (instancetype)initWithConfigurationJson:(MFWJson *)json
{
    self = [super init];
    if (self) {
        [self _parseJson:json];
    }
    return self;
}

- (void)_parseJson:(MFWJson *)json
{
    _json = json;
    
    // apng file
    self.apngFilePath = nil;
    self.scenario_apng_scale = [UIScreen mainScreen].scale;
    
    // fps
    self.scenario_fps = [json intValueForKey:@"fps"];
    
    // frames
    self.scenario_0_pull_static_frame = [json intValueForKey:@"s0_p"];
    self.scenario_1_trigger_static_frame = [json intValueForKey:@"s1_t"];
    
    self.scenario_2_release_start_frame = [json intValueForKey:@"s2_rs"];
    self.scenario_2_release_end_frame = [json intValueForKey:@"s2_re"];
    
    self.scenario_3_loading_loop_start_frame = [json intValueForKey:@"s3_lls"];
    self.scenario_3_loading_loop_end_frame = [json intValueForKey:@"s3_lle"];
    
    self.scenario_4_loading_finish_start_frame = [json intValueForKey:@"s4_lfs"];
    self.scenario_4_loading_finish_end_frame = [json intValueForKey:@"s4_lfe"];
    self.is_scenario_4_continue_from_scenario_3 = [json booleanValueForKey:@"s4_continue"];
    
    self.scenario_5_refresh_up_static_frame = [json intValueForKey:@"s5_ru"];
    
    // timer
    self.scenario_3_loading_loop_min_duration = [json doubleValueForKey:@"s3_ll_mdur"];
    if (self.scenario_3_loading_loop_min_duration < 0.001) {
        self.scenario_3_loading_loop_min_duration = (self.scenario_3_loading_loop_end_frame-self.scenario_3_loading_loop_start_frame+1)*self.scenario_frameInterval;
    }
    self.scenario_4_loading_finish_min_duration = [json doubleValueForKey:@"s4_lf_mdur"];
    if (self.scenario_4_loading_finish_min_duration < 0.001) {
        self.scenario_4_loading_finish_min_duration = (self.scenario_4_loading_finish_end_frame-self.scenario_4_loading_finish_start_frame+1)*self.scenario_frameInterval;
    }
    
    // color
    NSString *color = [json stringValueForKey:@"img_bgcolor"];
    self.apng_backgroundColor = [UIColor colorWithARGBHexString:color];
    color = [json stringValueForKey:@"refresh_bgcolor"];
    self.refresh_backgroundColor = [UIColor colorWithARGBHexString:color];
    
    // position
    self.is_scenario_position_customized = [json booleanValueForKey:@"is_pos_custom"];
    if (self.is_scenario_position_customized) {
        self.scenario_position_offset_x = [json floatValueForKey:@"pos_offx" defaultValue:0.f];
        self.is_scenario_position_forced_center_horizontal = [json booleanValueForKey:@"pos_force_center"];
        self.scenario_position_size_ratio = [json floatValueForKey:@"pos_size_ratio" defaultValue:1.0f];
    } else {
        self.scenario_position_offset_x = 0.f;
        self.is_scenario_position_forced_center_horizontal = NO;
        self.scenario_position_size_ratio = 1.f;
    }
    
    NSString *align = [json stringValueForKey:@"ver_align"]; // @"top", @"center", @"bottom"
    // apng图片下拉后位置的纵向对齐位置（顶部，居中，底部）
    if ([align isEqualToString:@"top"]) {
        self.scenario_apng_vertical_alignment = SAR_APNG_VerticalAlignment_Top;
    } else if ([align isEqualToString:@"center"]) {
        self.scenario_apng_vertical_alignment = SAR_APNG_VerticalAlignment_Center;
    } else if ([align isEqualToString:@"bottom"]) {
        self.scenario_apng_vertical_alignment = SAR_APNG_VerticalAlignment_Bottom;
    }
    
    self.refresh_vertical_offset = [json floatValueForKey:@"vertical_offy"];
    
    // refresh state
    self.scenario_loading_height = [json floatValueForKey:@"s_l_h" defaultValue:80];
    self.scenario_trigger_height = [json floatValueForKey:@"s_t_h" defaultValue:80];
    
    // metadata & extentions
    self.configurationId = [json stringValueForKey:@"s_id"];
    self.scenarioPageName = [json stringValueForKey:@"s_pn"];
    self.scenarioFilterKey = [json stringValueForKey:@"s_filter"];
    self.useForDebug = [json booleanValueForKey:@"debug"];
    self.isOnline = [json booleanValueForKey:@"online"];
    
    self.startTime = [json doubleValueForKey:@"start_date" defaultValue:0];
    self.endTime = [json doubleValueForKey:@"end_date" defaultValue:0];
    
    // 最后处理url，以为要根据url做ratio适配
    NSString *apng_320_1x_url = [json stringValueForKey:@"simg_320_1x" defaultValue:nil];
    NSString *apng_320_2x_url = [json stringValueForKey:@"simg_320_2x" defaultValue:nil];
    NSString *apng_375_2x_url = [json stringValueForKey:@"simg_375_2x" defaultValue:nil];
    NSString *apng_414_3x_url = [json stringValueForKey:@"simg_414_3x" defaultValue:nil];
    NSString *apng_iPad768_1x_url = [json stringValueForKey:@"simg_iPad768_1x_url" defaultValue:nil];
    NSString *apng_iPad768_2x_url = [json stringValueForKey:@"simg_iPad768_2x_url" defaultValue:nil];
    
    self.scenario_apng_url = nil;
    if (IS_IPAD == NO) {
        if (IS_SCREEN_5_5_OR_BIGGER)
        {
            if (apng_414_3x_url.length)
            {
                self.scenario_apng_url = apng_414_3x_url;
                self.scenario_apng_scale = 3.f;
            }
            else if (apng_375_2x_url.length)
            {
                self.scenario_apng_url = apng_375_2x_url;
                self.scenario_apng_scale = 2.f;
                if (self.is_scenario_position_customized == NO) {
                    self.is_scenario_position_customized = YES;
                    self.is_scenario_position_forced_center_horizontal = YES;
                    self.scenario_position_size_ratio *= 414.f/375.f;
                }
            }
            else if (apng_320_2x_url.length)
            {
                self.scenario_apng_url = apng_320_2x_url;
                self.scenario_apng_scale = 2.f;
                if (self.is_scenario_position_customized == NO) {
                    self.is_scenario_position_customized = YES;
                    self.is_scenario_position_forced_center_horizontal = YES;
                    self.scenario_position_size_ratio *= 414.f/320.f;
                }
            }
            else if (apng_320_1x_url.length)
            {
                self.scenario_apng_url = apng_320_1x_url;
                self.scenario_apng_scale = 2.f;
                if (self.is_scenario_position_customized == NO) {
                    self.is_scenario_position_customized = YES;
                    self.is_scenario_position_forced_center_horizontal = YES;
                    self.scenario_position_size_ratio *= 414.f/320.f;
                }
            }
        }
        else if (IS_SCREEN_4_7_OR_BIGGER)
        {
            if (apng_375_2x_url.length)
            {
                self.scenario_apng_url = apng_375_2x_url;
                self.scenario_apng_scale = 2.f;
            }
            else if (apng_414_3x_url.length)
            {
                self.scenario_apng_url = apng_414_3x_url;
                self.scenario_apng_scale = 3.f;
                if (self.is_scenario_position_customized == NO) {
                    self.is_scenario_position_customized = YES;
                    self.is_scenario_position_forced_center_horizontal = YES;
                    self.scenario_position_size_ratio *= 375.f/414.f;
                }
            }
            else if (apng_320_2x_url.length)
            {
                self.scenario_apng_url = apng_320_2x_url;
                self.scenario_apng_scale = 2.f;
                if (self.is_scenario_position_customized == NO) {
                    self.is_scenario_position_customized = YES;
                    self.is_scenario_position_forced_center_horizontal = YES;
                    self.scenario_position_size_ratio *= 375.f/320.f;
                }
            }
            else if (apng_320_1x_url.length)
            {
                self.scenario_apng_url = apng_320_1x_url;
                self.scenario_apng_scale = 1.f;
                if (self.is_scenario_position_customized == NO) {
                    self.is_scenario_position_customized = YES;
                    self.is_scenario_position_forced_center_horizontal = YES;
                    self.scenario_position_size_ratio *= 375.f/320.f;
                }
            }
        }
        else
        {
            if (apng_320_2x_url.length && IS_RETINA_2X())
            {
                self.scenario_apng_url = apng_320_2x_url;
                self.scenario_apng_scale = 2.f;
            }
            else if (apng_320_1x_url.length)
            {
                self.scenario_apng_url = apng_320_1x_url;
                self.scenario_apng_scale = 1.f;
            }
            else  if (apng_375_2x_url.length)
            {
                self.scenario_apng_url = apng_375_2x_url;
                self.scenario_apng_scale = 2.f;
                if (self.is_scenario_position_customized == NO) {
                    self.is_scenario_position_customized = YES;
                    self.is_scenario_position_forced_center_horizontal = YES;
                    self.scenario_position_size_ratio *= 320.f/375.f;
                }
            }
            else if (apng_414_3x_url.length)
            {
                self.scenario_apng_url = apng_414_3x_url;
                self.scenario_apng_scale = 3.f;
                if (self.is_scenario_position_customized == NO) {
                    self.is_scenario_position_customized = YES;
                    self.is_scenario_position_forced_center_horizontal = YES;
                    self.scenario_position_size_ratio *= 320.f/414.f;
                }
            }
        }
    }
    else
    {
        if (IS_RETINA()) {
            if (apng_iPad768_2x_url.length) {
                self.scenario_apng_url = apng_iPad768_2x_url;
                self.scenario_apng_scale = 2.f;
            } else if (apng_iPad768_1x_url.length) {
                self.scenario_apng_url = apng_iPad768_1x_url;
                self.scenario_apng_scale = 1.f;
            }
        } else {
            // 优先使用1x图片
            if (apng_iPad768_1x_url.length) {
                self.scenario_apng_url = apng_iPad768_1x_url;
                self.scenario_apng_scale = 1.f;
            } else if (apng_iPad768_2x_url.length) {
                self.scenario_apng_url = apng_iPad768_2x_url;
                self.scenario_apng_scale = 2.f;
            }
        }

        self.is_scenario_position_customized = YES;
        self.scenario_position_size_ratio = 1.f;
    }
}

@end
